package org.software.dao;

import org.software.entity.Class;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class ClassDataProvider {

    private static AtomicInteger classIdGenerator = new AtomicInteger(0);
//一个 AtomicInteger 类型的静态变量，用于生成唯一的类 ID。初始值为 0。
    private static List<Class> classes = new CopyOnWriteArrayList<>();

//添加类
    public static boolean classAdd(String className) {
        for (Class cls : classes) {
            if (cls.getClassName().equals(className)) {
                return false;//类名已存在
            }
        }
        Class newClass = new Class(classIdGenerator.incrementAndGet(), className, 0);
        classes.add(newClass);
        return true;//添加成功
    }
    //删除类
    public static boolean classDelete(String className) {
        return classes.removeIf(cls -> cls.getClassName().equals(className));
    }
//更新类名
    public static boolean classUpdate(String className,String newClassName) {
        for (Class cls : classes) {
            if(cls.getClassName().equals(className)){
                cls.setClassName(newClassName);
                return true;//更新成功
            }
        }
        return false;//未找到该类
    }
    //查询所有类
    public static List<Class> classQuery() {
        return new ArrayList<Class>(classes);//返回全部类的副本
    }
    //增加组数
    public static void incrementGroupCountForClass(String className) {
        for (Class cls : classes) {
            if(cls.getClassName().equals(className)){
                cls.setGroupCount(cls.getGroupCount()+1);
                break;//找到后退出
            }
        }
    }
//减少组数
    public static  void decrementGroupCountForClass(String className) {
        for (Class cls : classes) {
            if (cls.getClassName().equals(className)) {
                int currentCount = cls.getGroupCount();
                if (currentCount > 0) {
                    cls.setGroupCount(currentCount -1);
                }
                break;//找到后退出
            }
        }
    }
}
